package com.lumlord.webSocket;

import com.lumlord.exception.BusinessException;
import com.lumlord.util.WebSocketHelper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;


@ServerEndpoint(value = "/webSocket/{id}/{group}")
@RestController
@Slf4j
public class NettyWebSocketServer extends WebSocketHelper {


    /**
     * 连接建立成功调用的方法
     */

    @OnOpen
    public void onOpen(Session session, @PathParam("id") String id,@PathParam("group") String group) throws BusinessException {
        openSession(session, id,group);
        //sendMessage(userId, projectId,"握手成功！", null);
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        closeSession();
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息
     */
    @SneakyThrows
    @OnMessage
    public void onMessage(String message, Session session, @PathParam("id") String id, @PathParam("group") String group) throws BusinessException {
        log.info("收到来自窗口" + id + "的信息:" + message);
    }

    /**
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.info("发生错误 : "+error.getMessage());
        error.printStackTrace();
    }



}


